package Controller;

import model.fabricas.FabricaCaminos;
import model.fabricas.FabricaFantasmas;
import model.fabricas.FabricaPacman;
import model.interfaces.ICaminos;
import model.interfaces.IFantasma;
import model.interfaces.IPacman;
import model.utilidades.Vector;
import view.Drawable;
import view.DrawingArea;
import view.GhostGraphics;
import view.MapGraphics;
import view.PackGraphics;

public class App {
	static final int tamX = 800;
	static final int tamY = 580;
	//static final int tamX = 980;
	//static final int tamY = 620;
	
	static final int tasrmFontCode = 10;
	static final int tamFontMenu = 10;
	static final boolean debugModeOn = true;
	
	static final int posXFantasma = 410;
	static final int posYFantasma = 270;
	
	static final int posXPacman = 430;
	static final int posYPacman = 550;
	
	static final String beginningClip = "music/pacman_beginning.wav";
	static final String deathClip = "music/pacman_beginning.wav";
	
	public static void main(String args[]){
		MyWindow window = new MyWindow("ventana",tamX ,tamY);
		DrawingArea area = new DrawingArea("sprites/level/bg.png",tamX, tamY,debugModeOn);

		ICaminos nivel = createMap();

		IPacman pacman = FabricaPacman.crearPacman(new Vector(posXPacman,posYPacman), nivel);
		
		IFantasma buscador = FabricaFantasmas.crearFantasmaBuscador(new Vector(posXFantasma,posYFantasma), nivel);
		IFantasma temperamental = FabricaFantasmas.crearFantasmaBuscadorTemperamental(new Vector(posXFantasma+50,posYFantasma), nivel);
		IFantasma zonzo = FabricaFantasmas.crearFantasmaZonso(new Vector(posXFantasma+100,posYFantasma), nivel);
		IFantasma perezoso = FabricaFantasmas.crearFantasmaPerezoso(new Vector(posXFantasma+150,posYFantasma), nivel);
		
		area.addObject(createPackGraphics(pacman));
		area.addObject(createMapGraphics(nivel));
		area.addObject(createGhostGraphics("red",temperamental));
		area.addObject(createGhostGraphics("purple",buscador));
		area.addObject(createGhostGraphics("blue",perezoso));
		area.addObject(createGhostGraphics("orange",zonzo));
		
		window.addComponent(area, 1, 1, 0, 0);

		game(window,pacman,buscador,buscador,perezoso,zonzo);
	}
 
	public static void game(MyWindow window, IPacman pacman, IFantasma temperamental, IFantasma buscador, IFantasma perezoso, IFantasma zonzo){
		view.Audio.playSound(beginningClip); 
		while(true){
			pacman.vivir();
			
			buscador.interactuarCon(pacman);
			temperamental.interactuarCon(pacman);
			zonzo.interactuarCon(pacman);
			perezoso.interactuarCon(pacman);
			
			buscador.vivir(pacman);
			temperamental.vivir(pacman);
			zonzo.vivir(pacman);
			perezoso.vivir(pacman);
			
			window.redraw();
			try {
				Thread.sleep(80);
			} catch (InterruptedException e) {e.printStackTrace();}
		}
	}
	
	// creates the paths for the packman image list
	private static PackGraphics createPackGraphics(IPacman pMan){
		String imgs[] = { getPath("pack","up1"),getPath("pack","up2"),getPath("pack","up3"),getPath("pack","up2"),
						  getPath("pack","down1"),getPath("pack","down2"),getPath("pack","down3"),getPath("pack","down2"),
						  getPath("pack","left1"),getPath("pack","left2"),getPath("pack","left3"),getPath("pack","left2"),
						  getPath("pack","right1"),getPath("pack","right2"),getPath("pack","right3"),getPath("pack","right2"),
						  getPath("pack","dead1"),getPath("pack","dead2"),getPath("pack","dead3"),getPath("pack","dead4")};
		return new PackGraphics(imgs,pMan,debugModeOn);
	}

	// creates the paths for a ghost image list
	private static GhostGraphics createGhostGraphics(String color,IFantasma ghost){
		String imgs[] = {getPath(color,"up"),getPath(color,"down"),getPath(color,"left"),getPath(color,"right"),getPath(color,"dead")};
		return new GhostGraphics(imgs,ghost,debugModeOn);
	}
	
	// creates the path for the desired image
	private static String getPath(String color,String state){
		return new String("sprites/"+color+"/"+color+"_"+state+".png");
	}
	
	private static Drawable createMapGraphics(ICaminos map) {
		return new MapGraphics(map,debugModeOn);
	}
	
	// creates and retrurns a map
	private static ICaminos createMap() {
		ICaminos map = FabricaCaminos.crearCaminos();

		map.agregarCamino(new Vector(540,100),new Vector(640,100));
		map.agregarCamino(new Vector(640,100),new Vector(640,160));
		map.agregarCamino(new Vector(640,160),new Vector(540,160));
		map.agregarCamino(new Vector(540,160),new Vector(540,220));
		map.agregarCamino(new Vector(430,100),new Vector(540,100));
		map.agregarCamino(new Vector(540,100),new Vector(540,30));
		map.agregarCamino(new Vector(540,30),new Vector(750,30));
		map.agregarCamino(new Vector(750,30),new Vector(750,100));
		map.agregarCamino(new Vector(640,100),new Vector(750,100));
		map.agregarCamino(new Vector(750,160),new Vector(750,270));
		map.agregarCamino(new Vector(750,100),new Vector(750,160));
		map.agregarCamino(new Vector(750,160),new Vector(920,160));
		map.agregarCamino(new Vector(920,160),new Vector(920,100));
		map.agregarCamino(new Vector(920,30),new Vector(920,100));
		map.agregarCamino(new Vector(750,30),new Vector(920,30));
		map.agregarCamino(new Vector(750,100),new Vector(920,100));
		map.agregarCamino(new Vector(60,30),new Vector(430,30));
		map.agregarCamino(new Vector(430,30),new Vector(430,100));
		map.agregarCamino(new Vector(430,100),new Vector(330,100));
		map.agregarCamino(new Vector(220,100),new Vector(330,100));
		map.agregarCamino(new Vector(330,100),new Vector(330,160));
		map.agregarCamino(new Vector(330,160),new Vector(430,160));
		map.agregarCamino(new Vector(430,160),new Vector(430,220));
		map.agregarCamino(new Vector(60,30),new Vector(220,30));
		map.agregarCamino(new Vector(60,30),new Vector(60,100));
		map.agregarCamino(new Vector(60,100),new Vector(220,100));
		map.agregarCamino(new Vector(60,100),new Vector(60,160));
		map.agregarCamino(new Vector(60,160),new Vector(220,160));
		map.agregarCamino(new Vector(220,160),new Vector(220,100));
		map.agregarCamino(new Vector(220,100),new Vector(220,30));
		map.agregarCamino(new Vector(220,160),new Vector(220,270));
		map.agregarCamino(new Vector(60,550),new Vector(430,550));
		map.agregarCamino(new Vector(60,550),new Vector(60,495));		
		map.agregarCamino(new Vector(60,495),new Vector(120,495));
		map.agregarCamino(new Vector(120,495),new Vector(120,440));		
		map.agregarCamino(new Vector(120,440),new Vector(60,440));		
		map.agregarCamino(new Vector(60,440),new Vector(60,385));		
		map.agregarCamino(new Vector(340,220),new Vector(430,220));
		map.agregarCamino(new Vector(430,220),new Vector(485,220));
		map.agregarCamino(new Vector(485,220),new Vector(540,220));		
		map.agregarCamino(new Vector(540,220),new Vector(640,220));
		map.agregarCamino(new Vector(640,220),new Vector(640,270));
		map.agregarCamino(new Vector(485,220),new Vector(485,270));
		map.agregarCamino(new Vector(390,270),new Vector(485,270));
		map.agregarCamino(new Vector(485,270),new Vector(580,270));	
		map.agregarCamino(new Vector(340,270),new Vector(340,220));
		map.agregarCamino(new Vector(340,270),new Vector(340,325));		
		map.agregarCamino(new Vector(220,270),new Vector(340,270));
		map.agregarCamino(new Vector(220,385),new Vector(220,270));
		map.agregarCamino(new Vector(-10,270),new Vector(220,270));
		map.agregarCamino(new Vector(60,385),new Vector(220,385));
		map.agregarCamino(new Vector(340,385),new Vector(340,325));
		map.agregarCamino(new Vector(340,325),new Vector(640,325));
		map.agregarCamino(new Vector(640,325),new Vector(640,385));
		map.agregarCamino(new Vector(220,385),new Vector(340,385));	
		map.agregarCamino(new Vector(540,385),new Vector(640,385));
		map.agregarCamino(new Vector(640,385),new Vector(750,385));
		map.agregarCamino(new Vector(750,385),new Vector(750,270));
		map.agregarCamino(new Vector(750,270),new Vector(1000,270));
		map.agregarCamino(new Vector(750,270),new Vector(640,270));
		map.agregarCamino(new Vector(640,270),new Vector(640,325));
		map.agregarCamino(new Vector(340,385),new Vector(430,385));
		map.agregarCamino(new Vector(430,385),new Vector(430,440));		
		map.agregarCamino(new Vector(120,495),new Vector(220,495));
		map.agregarCamino(new Vector(220,495),new Vector(220,440));
		map.agregarCamino(new Vector(430,550),new Vector(540,550));
		map.agregarCamino(new Vector(430,550),new Vector(430,495));
		map.agregarCamino(new Vector(430,495),new Vector(340,495));
		map.agregarCamino(new Vector(340,495),new Vector(340,440));
		map.agregarCamino(new Vector(540,440),new Vector(540,385));
		map.agregarCamino(new Vector(540,550),new Vector(540,495));
		map.agregarCamino(new Vector(540,495),new Vector(635,495));
		map.agregarCamino(new Vector(635,495),new Vector(635,440));
		map.agregarCamino(new Vector(540,550),new Vector(920,550));
		map.agregarCamino(new Vector(920,550),new Vector(920,495));
		map.agregarCamino(new Vector(635,440),new Vector(750,440));
		
		map.agregarCamino(new Vector(220,385),new Vector(220,440));
		
		map.agregarCamino(new Vector(635,440),new Vector(540,440));		
		map.agregarCamino(new Vector(540,440),new Vector(430,440));
		map.agregarCamino(new Vector(430,440),new Vector(340,440));		
		map.agregarCamino(new Vector(340,440),new Vector(220,440));		
		map.agregarCamino(new Vector(920,495),new Vector(850,495));
		map.agregarCamino(new Vector(850,495),new Vector(850,440));		
		map.agregarCamino(new Vector(850,440),new Vector(920,440));		
		map.agregarCamino(new Vector(920,440),new Vector(920,385));		
		map.agregarCamino(new Vector(920,385),new Vector(750,385));
		map.agregarCamino(new Vector(750,385),new Vector(750,440));		
		map.agregarCamino(new Vector(850,495),new Vector(750,495));
		map.agregarCamino(new Vector(750,495),new Vector(750,440));
		
		return map;
	}
	
}
